<?php
require 'conn.php';
header('Content-Type: text/html;charset=utf-8');

if($_SERVER['REQUEST_METHOD']=='POST'){
	
	$cate = mysql_query('select * from cat');
	$cateArr = array();
	while($rs = mysql_fetch_array($cate)){
		$cateArr[$rs['userid']][] = array('id'=>$rs['id'],'name'=>$rs['catname'],'userid'=>$rs['userid'],'pid'=>$rs['pid'],'addtime'=>$rs['addtime'],'default'=>$rs['defaultcate']);
	}

	$fav = mysql_query('select * from fav order by id desc');
	$favArr = array();
	while($rs = mysql_fetch_array($fav)){
		$favArr[$rs['userid']][] = array('id'=>$rs['id'],'host'=>$rs['host'],'userid'=>$rs['userid'],'url'=>$rs['url'],'addtime'=>$rs['addtime'],'title'=>$rs['title'],'cateid'=>$rs['cateid'],'clicktimes'=>$rs['clicktimes'],'viewtime'=>$rs['viewtime'],'actionip'=>$rs['actionip'],'import'=>$rs['import']);
	}
		
	$result = mysql_query('select * from fuser');
	while($rs = mysql_fetch_array($result)){
	
		$dom = new DomDocument('1.0','utf-8');
		
		$root = $dom -> createElement('root');
		$username = $dom -> createElement('username');
		$password = $dom -> createElement('password');
		$username -> appendChild( $dom -> createTextNode($rs['username']) );
		$password -> appendChild( $dom -> createTextNode($rs['password']) );
		
		$addtime = $dom -> createElement('addtime');
		$addtime -> appendChild( $dom -> createTextNode($rs['addtime']) );
		
		$email = $dom -> createElement('email');
		$email -> appendChild( $dom -> createTextNode($rs['email']) );
		
		$actionip = $dom -> createElement('actionip');
		$actionip -> appendChild( $dom -> createTextNode($rs['actionip']) );
		
		$category = $dom -> createElement('category');
		foreach($cateArr[$rs['id']] as $userCat){
			$cat = $dom -> createElement('cat');
			$cat -> setAttribute('id',$userCat['id']);
			$cat -> setAttribute('pid',$userCat['pid']);
			$cat -> setAttribute('addtime',$userCat['addtime']);
			$cat -> setAttribute('default',$userCat['default']);
			$cat -> appendChild( $dom -> createTextNode($userCat['name']) );
			$category -> appendChild($cat);
		}
		$favorites = $dom -> createElement('favorites');
		foreach($favArr[$rs['id']] as $userFav){
			$fav = $dom -> createElement('fav');
			$fav -> setAttribute('id',$userFav['id']);
			//$fav -> setAttribute('host',$userFav['host']);
			$fav -> setAttribute('url',$userFav['url']);
			$fav -> setAttribute('viewtime',$userFav['viewtime']);
			$fav -> setAttribute('catid',$userFav['cateid']);
			$fav -> setAttribute('actionip',$userFav['actionip']);
			$fav -> setAttribute('import',$userFav['import']);
			$fav -> setAttribute('addtime',$userFav['addtime']);
			$fav -> setAttribute('clicktimes',$userFav['clicktimes']);
			$fav -> setAttribute('title',$userFav['title']);
			
			$favorites -> appendChild($fav);
		}
		
		$root -> appendChild($username);
		$root -> appendChild($password);
		$root -> appendChild($addtime);
		$root -> appendChild($email);
		$root -> appendChild($actionip);
		
		$root -> appendChild($category);
		$root -> appendChild($favorites);
		
		$root -> setAttribute('id',$rs['id']);
		$dom -> appendChild($root);
		
		$dom -> formatOutput = true;
		
		$dom -> save('userdata/'.iconv('UTF-8', 'GB2312' ,strtolower($rs['username'])).'.fml');
	
	}
	echo 'OK';
	exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <input type="submit" name="button" id="button" value="提交" />
</form>
</body>
</html>
